/*

 Very basic example of ASNIDom http templating server
 
 This server will respond with either html or ANSI codes depending on the user-agent and accept headers
 of the incoming request

*/


var ANSIdom = require('../lib/ANSIdom').ANSIdom,
    http    = require('http'),
    fs      = require('fs');

var dom = new ANSIdom();

//
// Load a html template
//
var template = fs.readFileSync('./templates/index.html').toString();

dom.init(function(err){
  
  if (err) {
    throw err;
  }

  http.createServer(function(req, res){


    switch (req.url) {

      case '/':
        //
        // Simple accept based routing, if text/html wasn't explicity sent,
        // respond with text/plain and ANSI codes
        //
        if(req.headers.accept.indexOf('text/html') === -1) {
          dom.render(template, res);
          res.end();
        } else {
          res.end(template);
        }

      break;

      default:
        res.end('404');
      break;

    }


  }).listen(8000);


});


